home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / LDelayedTask / UDelayedTaskQueue.cp < prev    next >
Encoding:
Text File  |  1995-08-28  |  3.7 KB  |  149 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    UDelayedTaskQueue.cp            © 1995, Éric Forget. All rights reserved.
  3. // ===========================================================================
  4. //    
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    UDelayedTaskQueue is an utility class to help LDelayedTask.
  13. //
  14. // ---------------------------------------------------------------------------
  15. //
  16. //    Instruction Notes:
  17. //    -----------------
  18. //
  19. //    Look in the class LDelayedTask for instructions.
  20. //
  21. // ---------------------------------------------------------------------------
  22.  
  23. #ifdef PowerPlant_PCH
  24. #include PowerPlant_PCH
  25. #endif
  26.  
  27. #include "UDelayedTaskQueue.h"
  28. #include <LDynamicArray.h>
  29. #include "LDelayedTask.h"
  30.  
  31.  
  32. // ---------------------------------------------------------------------------
  33. //        • Static members
  34. // ---------------------------------------------------------------------------
  35.  
  36.  
  37. LDynamicArray*        UDelayedTaskQueue::sTaskQ = nil;
  38. UDelayedTaskQueue*    UDelayedTaskQueue::sDelayedTaskQueue = nil;
  39.  
  40.  
  41.  
  42. // ---------------------------------------------------------------------------
  43. //        • UDelayedTaskQueue
  44. // ---------------------------------------------------------------------------
  45. //    Default constructor
  46.  
  47. UDelayedTaskQueue::UDelayedTaskQueue()
  48. {
  49. }
  50.  
  51.  
  52. // ---------------------------------------------------------------------------
  53. //        • ~UDelayedTaskQueue
  54. // ---------------------------------------------------------------------------
  55. //    Destructor
  56.  
  57. UDelayedTaskQueue::~UDelayedTaskQueue()
  58. {
  59.     if(sTaskQ != nil) {
  60.     
  61.         LTask    *theTask;
  62.         
  63.         for(Int32 i = sTaskQ->GetCount(); i > 0; i--) {
  64.         
  65.             sTaskQ->FetchItemAt(i, &theTask);
  66.             Assert_(!theTask->IsExecuting());
  67.             
  68.             delete theTask;
  69.             sTaskQ->RemoveItemsAt(1, i);
  70.         }
  71.         
  72.         delete sTaskQ;
  73.     }
  74. }
  75.  
  76.  
  77. // ---------------------------------------------------------------------------
  78. //        • AddTask
  79. // ---------------------------------------------------------------------------
  80. //    Add Task in the to delete queue
  81.  
  82. void
  83. UDelayedTaskQueue::AddTask(
  84.     LDelayedTask    *inTask)
  85. {
  86.     if (sTaskQ == nil) {            // Create queue if it doesn't exist
  87.         
  88.         sTaskQ = new LList;
  89.     }
  90.     
  91.     if (sDelayedTaskQueue == nil) {    // Create object if it doesn't exist
  92.         
  93.         sDelayedTaskQueue = new UDelayedTaskQueue;
  94.     }
  95.     
  96.                                     // Add to end of the to delete queue
  97.     sTaskQ->InsertItemsAt(1, arrayIndex_Last, &inTask);
  98.     sDelayedTaskQueue->StartRepeating();
  99. }
  100.  
  101.  
  102. // ---------------------------------------------------------------------------
  103. //        • RemoveTask
  104. // ---------------------------------------------------------------------------
  105.  
  106. void
  107. UDelayedTaskQueue::RemoveTask(
  108.     LDelayedTask    *inTask)
  109. {
  110.     Int32    index = sTaskQ->FetchIndexOf(&inTask);
  111.     
  112.     
  113.     if(index != arrayIndex_Bad) {
  114.     
  115.         sTaskQ->RemoveItemsAt(1, index);
  116.         
  117.         if(sTaskQ->GetCount() == 0) {
  118.         
  119.             delete sTaskQ;
  120.             sTaskQ = nil;
  121.             delete sDelayedTaskQueue;
  122.             sDelayedTaskQueue = nil;
  123.         }
  124.     }
  125. }
  126.  
  127.  
  128. // ---------------------------------------------------------------------------
  129. //        • SpendTime
  130. // ---------------------------------------------------------------------------
  131.  
  132. void
  133. UDelayedTaskQueue::SpendTime(
  134.     const EventRecord &/*inMacEvent*/)
  135. {
  136.     LDelayedTask    *theTask;
  137.     
  138.     
  139.     for(Int32 i = sTaskQ->GetCount(); i > 0; i--) {
  140.     
  141.         sTaskQ->FetchItemAt(i, &theTask);
  142.         
  143.         if(theTask->IsItTimeToExecute()) {
  144.         
  145.             theTask->ExecuteTask();
  146.         }
  147.     }
  148. }
  149.